package helper

import (
	"chatgpt/helper/code"
	"chatgpt/model/app"
	"crypto/md5"
	"fmt"
	"io"
)

var LoginAccount loginAccount

type loginAccount struct {
}

func (self *loginAccount) AccountPassword(password string) string {
	h := md5.New()
	io.WriteString(h, password+"")
	return fmt.Sprintf("%x", h.Sum(nil))
}

func (self *loginAccount) LengthCheck(passwd string) code.CODENO {
	if len(passwd) < 6 {
		return code.REQ_PASSWD_SHORT_ERR
	}
	return code.SUCCESS
}

func (self *loginAccount) PasswdEqual(passwd, repeat string) bool {
	if (passwd) == self.AccountPassword(repeat) {
		return true
	}
	return false
}

func (self *loginAccount) PasswdCheck(usr *app.User, repeat string, method string) code.CODENO {
	switch method {
	case "", "account":
		if self.PasswdEqual(usr.Passwd, repeat) {
			return code.SUCCESS
		}
		return code.LOGIN_ID_PASS_ERR
	}
	return code.SERVER_ERR
}
